/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.telsoft.security.managedbean.function;

import com.telsoft.security.business.SEC_BU_Group;
import com.telsoft.security.business.SEC_BU_USER_GROUP;
import com.telsoft.security.business.SEC_BU_User;
import com.telsoft.security.entity.SEC_ETT_Group;
import com.telsoft.security.entity.SEC_ETT_User;
import com.telsoft.security.entity.SEC_ETT_UserGroup;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.xml.bind.JAXBException;

/**
 *
 * @author nguyenthang
 */
@ManagedBean(name = "func_admin_user")
@ViewScoped
public class SEC_FUNC_User implements Serializable {

    private List<String> activeState;
    private List<SEC_ETT_UserGroup> listUG;
    private SEC_ETT_UserGroup user;
    private SEC_ETT_UserGroup userItem;
    private SEC_ETT_User item;
    private SEC_ETT_Group curGr;
    private List<SEC_ETT_Group> listGroup;
    private Boolean edit;
    private Boolean add;
    private Boolean delete;
    private Boolean isSelect;
    private String statusDialog;
    private Boolean ctrlEdit;
    private Boolean ctrlAdd;
    private Boolean ctrlDel;
    private List<String> listStatus;

    public SEC_FUNC_User() throws JAXBException {
        listStatus = new ArrayList<String>();
        activeState = new ArrayList<String>();
        listUG = new ArrayList<SEC_ETT_UserGroup>();
        listUG = SEC_BU_USER_GROUP.getAllUG();
        listGroup = SEC_BU_Group.getAllGroup();
        userItem = new SEC_ETT_UserGroup();
        edit = false;
        add = false;
        isSelect = false;
        user = new SEC_ETT_UserGroup();
        curGr = new SEC_ETT_Group();
        ctrlAdd = true;
        ctrlDel = false;
        ctrlEdit = false;
        for (SEC_ETT_UserGroup g : listUG) {
            if (g.getUSER_STATUS() == 0) {
                activeState.add("Disabled");
            } else {
                activeState.add("Enabled");
            }
        }
        
    }

    public void handEdit() {
        edit = true;
        add = false;
        statusDialog = "Edit Infomation of User";
        userItem = user;
        listGroup = new ArrayList<SEC_ETT_Group>();
        curGr = SEC_BU_Group.getGroupByName(user.getGROUP_NAME());
        listGroup.add(curGr);
        List<SEC_ETT_Group> templist = SEC_BU_Group.getAllGroup();
        for (int i = 0; i < templist.size(); i++) {
            if (templist.get(i).getGROUP_ID() == curGr.getGROUP_ID()) {
            } else {
                listGroup.add(templist.get(i));
            }
        }
        ctrlAdd = true;
        ctrlEdit = false;
        ctrlDel = false;
    }

    public void handAdd() {
        edit = false;
        add = true;
        statusDialog = "Add New User";
        user = new SEC_ETT_UserGroup();
        curGr = new SEC_ETT_Group();
        ctrlAdd = true;
        ctrlEdit = false;
        ctrlDel = false;
    }

    public void handDel() {
        SEC_BU_USER_GROUP.deleteUGByUserID(user.getUSER_ID());
        SEC_BU_User.deleteUser(user.getUSER_ID());
        // Update List
        listUG = SEC_BU_USER_GROUP.getAllUG();
        ctrlDel = false;
        ctrlEdit = false;
        ctrlAdd = true;
    }

    public void handOk() {
        user.setGROUP_NAME(curGr.getGROUP_NAME());
        item = new SEC_ETT_User();
//        item.setUSER_ID(user.getUSER_ID());
        item.setUSER_NAME(user.getUSER_NAME());
        item.setFULL_NAME(user.getFULL_NAME());
        item.setSTATUS(user.getUSER_STATUS());
        item.setEMAIL(user.getUSER_EMAIL());
        item.setPASSWORD(user.getUSER_PASSWORD());

        if (edit) {
            activeState = new ArrayList<String>();
            SEC_BU_USER_GROUP.updateUserGroup(user.getUSER_ID(), curGr.getGROUP_ID());
            SEC_BU_User.editUser(user.getUSER_ID(), item);
            listUG = SEC_BU_USER_GROUP.getAllUG();
            for (SEC_ETT_UserGroup g : listUG) {
                if (g.getUSER_STATUS() == 0) {
                    activeState.add("Disabled");
                } else {
                    activeState.add("Enabled");
                }
            }
            user = new SEC_ETT_UserGroup();
        }
        if (add) {
            activeState = new ArrayList<String>();
            SEC_BU_User.addUser(item);
            SEC_ETT_User temp = SEC_BU_User.getUserByName(user.getUSER_NAME());
            SEC_BU_USER_GROUP.addUserGroup(temp.getUSER_ID(), curGr.getGROUP_ID());
            listUG = SEC_BU_USER_GROUP.getAllUG();
            for (SEC_ETT_UserGroup g : listUG) {
                if (g.getUSER_STATUS() == 0) {
                    activeState.add("Disabled");
                } else {
                    activeState.add("Enabled");
                }
            }
            user = new SEC_ETT_UserGroup();
        }
        ctrlAdd = true;
        ctrlEdit = true;
        ctrlDel = true;
    }
    public String showOrDide(){
        String showHide = null;
        if (add){
            showHide = "UserDialog.show()";
        }
        if (edit){
            showHide = "UserDialog.hide()";
        } 
        return showHide;
    }
    public void setCtrlEdit(Boolean ctrlEdit) {
        this.ctrlEdit = ctrlEdit;
    }

    public void setCtrlAdd(Boolean ctrlAdd) {
        this.ctrlAdd = ctrlAdd;
    }

    public void setCtrlDel(Boolean ctrlDel) {
        this.ctrlDel = ctrlDel;
    }

    public Boolean getCtrlEdit() {
        return ctrlEdit;
    }

    public Boolean getCtrlAdd() {
        return ctrlAdd;
    }

    public Boolean getCtrlDel() {
        return ctrlDel;
    }

    public void onRowSelect() {
        isSelect = true;
        ctrlDel = true;
        ctrlEdit = true;
    }

    public void cancel() {
        if (!isSelect) {
            ctrlDel = false;
            ctrlEdit = false;
        } else {
            ctrlDel = true;
            ctrlEdit = true;
        }
    }

    public void setIsSelect(Boolean isSelect) {
        this.isSelect = isSelect;
    }

    public Boolean getIsSelect() {
        return isSelect;
    }

    public List<String> getActiveState() {
        return activeState;
    }

    public void setActiveState(List<String> activeState) {
        this.activeState = activeState;
    }

    public void setListUG(List<SEC_ETT_UserGroup> listUG) {
        this.listUG = listUG;
    }

    public List<SEC_ETT_UserGroup> getListUG() {
        return listUG;
    }

    public Boolean getEdit() {
        return edit;
    }

    public Boolean getAdd() {
        return add;
    }

    public Boolean getDelete() {
        return delete;
    }

    public String getStatusDialog() {
        return statusDialog;
    }

    public void setEdit(Boolean edit) {
        this.edit = edit;
    }

    public void setAdd(Boolean add) {
        this.add = add;
    }

    public void setDelete(Boolean delete) {
        this.delete = delete;
    }

    public void setStatusDialog(String statusDialog) {
        this.statusDialog = statusDialog;
    }

    public void setUser(SEC_ETT_UserGroup user) {
        this.user = user;
    }

    public void setItem(SEC_ETT_User item) {
        this.item = item;
    }

    public SEC_ETT_UserGroup getUser() {
        return user;
    }

    public SEC_ETT_User getItem() {
        return item;
    }

    public void setUserItem(SEC_ETT_UserGroup userItem) {
        this.userItem = userItem;
    }

    public void setListGroup(List<SEC_ETT_Group> listGroup) {
        this.listGroup = listGroup;
    }

    public SEC_ETT_UserGroup getUserItem() {
        return userItem;
    }

    public void setListStatus(List<String> listStatus) {
        this.listStatus = listStatus;
    }

    public List<String> getListStatus() {
        return listStatus;
    }

    public List<SEC_ETT_Group> getListGroup() {
        return listGroup;
    }

    public SEC_ETT_Group getCurGr() {
        return curGr;
    }

    public void setCurGr(SEC_ETT_Group curGr) {
        this.curGr = curGr;
    }
}
